﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpCreateRefPlaneByPickCmd.h"
#include "PickCurveCreateReferencePlaneBehavior.h"
#include "GbmpUiPlatform.h"
#include "GbmpUiPlatformCommandRegister.h"
#include "GbmpUiPlatformCommandIds.h"
#include "CmdStatusUtils.h"
#include "IUiView.h"
#include "IUserTransaction.h"
#include "IModelView.h"
#include "GbmpModelViewUtil.h"
#include "Coordinate3d.h"
#include "AlgorithmProject.h"
#include "ReferencePlaneUtils.h"
#include "IReferencePlane.h"
#include "IElementBasicInformation.h"
#include "IPlane.h"
#include "ICurve2d.h"
#include "ILine2d.h"
#include "ActionDrawCurveByPickCreator.h"
#include "PickCurveDrawElementBehavior.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"


using namespace gcmp;

GbmpCreateRefPlaneCmdByPick::GbmpCreateRefPlaneCmdByPick() : GbmpCommandBase(ID_COM_DRAW_STRUCTURE_REFERENCEPLANE_BY_Pick)
{
}

GbmpCreateRefPlaneCmdByPick::~GbmpCreateRefPlaneCmdByPick()
{
}

gcmp::OwnerPtr<IAction> GbmpCreateRefPlaneCmdByPick::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    // 配置Action
    OwnerPtr<PickCurveDrawElementBehavior> opRefPlanePickBehavior = NEW_AS_OWNER_PTR(PickCurveCreateReferencePlaneBehavior, UiDocumentViewUtils::GetCurrentDocument());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opRefPlanePickBehavior, L"opBeamBehavior为空",L"GDMPLab",L"2024-03-30");
    opRefPlanePickBehavior->SetOnCurvePickedCallback(PickCurveDrawElementBehavior::CreateOnCurvePickedCallback(&GbmpCreateRefPlaneCmdByPick::OnCurvePicked, this));

    OwnerPtr<ActionDrawCurveByPickInput> pickCurveActionInput = NEW_AS_OWNER_PTR(ActionDrawCurveByPickInput, TransferOwnership(opRefPlanePickBehavior));
    OwnerPtr<IAction> opPickCurveAction = ActionDrawCurveByPickCreator::Create(TransferOwnership(pickCurveActionInput));

    return TransferOwnership(opPickCurveAction);
}

bool GbmpCreateRefPlaneCmdByPick::OnCurvePicked(IUiView* pCurrentView, OwnerPtr<ICurve2d> opCurve2d)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pCurrentView, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opCurve2d, L"opCurve2d为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    //IDocument* pDoc = pCurrentView->GetUiDocument()->GetDbDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    IModelView* pModelView = pCurrentView->GetModelView();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    gcmp::Coordinate3d workPlaneCoord = GbmpModelViewUtil::GetWorkPlaneCoordinate(pModelView);
    Vector3d planeNormal = -pModelView->GetViewDirection();
    gcmp::OwnerPtr<gcmp::IPlane> opWorkPlane = gcmp::IPlane::Create(workPlaneCoord.GetOrigin(), planeNormal);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opWorkPlane, L"pPlane为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> ut = IUserTransaction::Create(pDoc, GBMP_TR(L"创建参照平面"));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(ut, L"ut为空",L"GDMPLab",L"2024-03-30");

    {
        if (opCurve2d->GetType() == GeometryType::Line2d)
        {
            const ILine2d* pLine2d = quick_cast<ILine2d>(opCurve2d.get());
            DBG_WARN_AND_RETURN_FALSE_UNLESS(pLine2d, L"pLine2d为空",L"GDMPLab",L"2024-03-30");

            Vector3d startPt(pLine2d->GetStartPoint(), 0);
            Vector3d endPt(pLine2d->GetEndPoint(), 0);

            gcmp::Vector3d projectStartPt, projectEndPt;
            Vector3d pointProjectedOnLevelPlane;
            Vector2d uvParameter;
            AlgorithmProject::Project(startPt, opWorkPlane.get(), uvParameter, projectStartPt);
            AlgorithmProject::Project(endPt, opWorkPlane.get(), uvParameter, projectEndPt);

            IReferencePlane* pReferencePlane = ReferencePlaneUtils::CreateByPoints(pDoc, projectStartPt, projectEndPt, opWorkPlane->GetNormal());
            DBG_WARN_AND_RETURN_FALSE_UNLESS(pReferencePlane != nullptr, L"参照平面创建失败",L"GDMPLab",L"2024-03-30");
            pReferencePlane->SetReferencePlaneType(ReferencePlaneType::UserDefined);

            gcmp::IElementBasicInformation* pElementBasicInformation = pReferencePlane->GetBasicInformation();
            DBG_WARN_AND_RETURN_FALSE_UNLESS(pElementBasicInformation, L"pElementBasicInformation为空",L"GDMPLab",L"2024-03-30");
            pElementBasicInformation->SetName(GBMP_TR(L"参照平面"));
        }
        else
        {
            DBG_WARN_AND_RETURN_FALSE_UNLESS(false, L"暂不支持直线以外的线型",L"GDMPLab",L"2024-03-30");
        }
    }
    ut->Commit();
    return true;
}

bool GbmpCreateRefPlaneCmdByPick::IsVisible() const
{
    CMD_ONLY_VISIBLE_IN_DOCUMENT_ENVIRONMENT
}
bool GbmpCreateRefPlaneCmdByPick::IsEnabled() const
{
    if (!CommandBase::IsEnabled())
        return false;
    return true;
}

REGISTER_GM_COMMAND(GbmpCreateRefPlaneCmdByPick)
